package org.syncsoft.overalt.domain.document.entity;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringEscapeUtils;
import org.syncsoft.overalt.domain.common.entity.IEntity;
import org.syncsoft.overalt.domain.community.entity.Community;
import org.syncsoft.overalt.domain.tag.entity.Tag;
import org.syncsoft.overalt.domain.user.entity.User;

/**
 * Class of domain that represents the a.m.i.g.o.s. document
 * @author tptfc
 *
 */
public class Document implements IEntity {
	
	private int code;
	
	private String title;
	
	private String description;

	private String filepath;
	
	private String fileName;
	
	private boolean commitable;
	
	private int version;
	
	private Set<Tag> tags;
	
	private User user;
	
	private Set<Community> communities;
	
	private Policy policy;
	
	{
		setTags(new HashSet<Tag>());
		setCommunities(new HashSet<Community>());
	}
	
	/**
	 * 
	 * @param title the document title
	 * @param description the document description
	 */
	public Document(String title, String description) {
		setTitle(title);
		setDescription(description);
	}
	
	public Document(String filepath, boolean commitable){
		setFilepath(filepath);
		setCommitable(commitable);
	}
	
	public Document(String filePath) {
		setFilepath(filePath);
	}
	
	public Document(int code) {
		setCode(code);
	}
	
	public Document() {
	}
	
	/**
	 * @precondition tag != null
	 * @postcondition	getTags will contain the tag
	 * 					not will contain duplicate tags
	 * 
	 * @param tag
	 */
	public void addTag(Tag tag) {
		if (tag != null) {
			getTags().add(tag);
		}
	}
	
	/**
	 * @precondition community != null
	 * @param community
	 */
	public void addCommunity(Community community) {
		if (community != null) {
			getCommunities().add(community);
		}
	}
	
	/**
	 * @precondition tag != null
	 * @postcondition removed of getTags the tag
	 * @param tag
	 */
	public void removeTag(String tag) {
		getTags().remove(tag);
	}
	
	//	GETS AND SETS
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = StringEscapeUtils.unescapeHtml(title);
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = StringEscapeUtils.unescapeHtml(description);
	}

	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}

	public boolean isCommitable() {
		return commitable;
	}

	public void setCommitable(boolean commitable) {
		this.commitable = commitable;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	public Set<Community> getCommunities() {
		return communities;
	}

	public void setCommunities(Set<Community> communities) {
		this.communities = communities;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Document && ((Document)obj).getCode() == getCode()) {
			return true;
		}
		return false;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Policy getPolicy() {
		return policy;
	}

	public void setPolicy(Policy policy) {
		this.policy = policy;
	}
	
	
	
}
